#!/usr/bin/python
# -*- encoding: utf-8 -*-
'''
@File    :   
@Time    :   // ::
@Author  :   Malygos 
@Version :   1.0
@Contact :   751756061@qq.com
@WebSite :   https://gitee.com/malygos_l/python_homework
'''
# Start typing your code from here
'''引入数据库'''

from sqlalchemy import Column, String, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

# 创建对象的基类:
Base = declarative_base()


# 定义User对象:
class User(Base):
    # 表的名字:
    __tablename__ = 'job'

    # 表的结构:
    jobname = Column(String(255))
    company = Column(String(255), primary_key=True)
    place = Column(String(255))
    salary = Column(String(255))
    time = Column(String(20))


# 初始化数据库连接:
engine = create_engine('mysql+mysqlconnector://root:18311010@localhost:3306/test')
# 创建DBSession类型:
DBSession = sessionmaker(bind=engine)

session = DBSession()


# 写入数据库函数
def write(spider_data):     # 传入爬取数据，写入数据库
    try:
        new_person = User(jobname=spider_data["Jobname"],
                          company=spider_data["Company"],
                          place=spider_data["Place"],
                          salary=spider_data["salary"],
                          time=spider_data["time"])
        session.add(new_person)
        session.commit()
        session.close()
    except Exception as e:
        raise e
